﻿using System;
using KillBill.Business.Entities;
using KillBill.Common.View;
using KillBill.Presentation.Common;
using KillBill.Presentation.Modules.Administration.AdministrationServiceReference;
using KillBill.Presentation.Modules.Administration.IView;
using KillBill.Services.DTO;

namespace KillBill.Presentation.Modules.Administration.Presenter
{
    public class CustomerAddEditPresenter
    {
        public ICustomerAddEdit View { get; set; }

        private readonly AdministrationServiceClient _service;


        public CustomerAddEditPresenter(ICustomerAddEdit view)
        {
            _service = new AdministrationServiceClient();
            View = view;
            View.IsEditing = false;
        }

        public CustomerAddEditPresenter(ICustomerAddEdit view, Customer customer)
        {
            _service = new AdministrationServiceClient();
            View = view;
            View.IsEditing = true;
        }

        public void Save()
        {
            KillBillDTO dto = View.IsEditing ? 
                _service.UpdateCustomer(View.Customer, Context.SessionID) : 
                _service.AddCustomer(View.Customer, Context.SessionID);
            View.ShowMessage((dto.Success) ? 
                dto.Messages[0] : dto.Messages[0] + " " + 
                dto.Messages[1]);
        }
    }
}
